//
//  SISSRecommendedPassengerDetailViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/7/5.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSRecommendedPassengerDetailViewController.h"
#import "SISSPassengerCell.h"
#import "SISSPassengerResourceDetailVC.h"
#import "SISSNetworkingManager+HouseProgress.h"
#import "SISSNetworkingManager+Home.h"

static NSString *passengerCellReuseID = @"passengerCellReuseID";
@interface SISSRecommendedPassengerDetailViewController ()<UITableViewDataSource,UITableViewDelegate>


@property (weak, nonatomic) IBOutlet UILabel *nameAndPurposeLabel;
@property (weak, nonatomic) IBOutlet UILabel *regionAndCommunityLabel;
@property (weak, nonatomic) IBOutlet UILabel *budgetLabel;
@property (weak, nonatomic) IBOutlet UILabel *houseTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *areaLabel;
@property (weak, nonatomic) IBOutlet UILabel *intentionFloorLabel;
@property (weak, nonatomic) IBOutlet UILabel *decorationLabel;
@property (weak, nonatomic) IBOutlet UILabel *directionLabel;
@property (weak, nonatomic) IBOutlet UILabel *purchasePurposeLabel;
@property (weak, nonatomic) IBOutlet UILabel *otherDescriptionLabel;

@property (weak, nonatomic) IBOutlet UITableView *tableView;


/** 客源数组 */
@property (strong, nonatomic) NSArray<SISSPassengerResourceModel *> *passengerResourceArr;

@end

@implementation SISSRecommendedPassengerDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight = 66;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSPassengerCell" bundle:nil] forCellReuseIdentifier:passengerCellReuseID];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.otherDescriptionLabel.text = @" ";
    [self requestPassengerResourceData];
}



- (void)setPassengerModel:(SISSPassengerResourceModel *)passengerModel {
    [self.view layoutIfNeeded];
    _passengerModel = passengerModel;
    
    if (passengerModel.demandType != 2) {
        
        // 根据身份证判断显示 先生、女士
        NSString *fisrtName = [passengerModel.name substringToIndex:1];
        NSInteger idNum = [[passengerModel.idNumber substringWithRange:NSMakeRange(passengerModel.idNumber.length - 2, 1)] integerValue];
        
        NSString *genderStr = (idNum % 2) == 0 ? @"女士" : @"先生";
        self.nameAndPurposeLabel.text = [NSString stringWithFormat:@"%@%@  %@",fisrtName,genderStr,@"求购"];
    } else {
        self.nameAndPurposeLabel.text = [NSString stringWithFormat:@"%@  %@",passengerModel.name,@"求购"];
    }
    
    self.regionAndCommunityLabel.text = passengerModel.neighbourhood ? [NSString stringWithFormat:@"%@-%@ %@",passengerModel.region,passengerModel.regionDetail,passengerModel.neighbourhood] : [NSString stringWithFormat:@"%@-%@",passengerModel.region,passengerModel.regionDetail];
    self.budgetLabel.text = [NSString stringWithFormat:@"%.0f-%.0f万",passengerModel.minPrice,passengerModel.maxPrice];
    self.houseTypeLabel.text = passengerModel.houseType;
    self.areaLabel.text = [NSString stringWithFormat:@"%.0f-%.0f㎡",passengerModel.minArea,passengerModel.maxArea];
    self.intentionFloorLabel.text = (passengerModel.minFloor != 0 && passengerModel.maxFloor != 0) ? [NSString stringWithFormat:@"%zd-%zd层",passengerModel.minFloor,passengerModel.maxFloor] : @"暂无数据";
    self.decorationLabel.text = passengerModel.decoration.length > 0 ? passengerModel.decoration : @"暂无数据";
    self.directionLabel.text = passengerModel.direction.length > 0 ? passengerModel.direction : @"暂无数据";
    self.purchasePurposeLabel.text = passengerModel.purpose.length > 0 ? passengerModel.purpose : @"暂无数据";
    self.otherDescriptionLabel.text = passengerModel.otherdesc.length > 0 ? passengerModel.otherdesc : @"暂无数据";


}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.passengerResourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SISSPassengerCell *cell = [tableView dequeueReusableCellWithIdentifier:passengerCellReuseID];
    cell.isHideButton = YES;
    
    cell.passengerModel = self.passengerResourceArr[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SISSPassengerResourceDetailVC *vc = [[SISSPassengerResourceDetailVC alloc] init];
    vc.title = @"客源详情";
    vc.isHideBottomButton = YES;
    SISSPassengerResourceModel *passengerModel = self.passengerResourceArr[indexPath.row];
    if (!passengerModel.read) {
        
        [kNetworkManager setPassengerResourceReadWithPassengerId:passengerModel.ID success:^(id responseObject) {
            
            if ([responseObject[@"code"] integerValue] == 200) {
                NSLog(@"插入成功！");
            }
        } failed:^(NSError *error) {
            
            NSLog(@"%@",error);
        }];
    }
    vc.passengerModel = passengerModel;
    self.passengerResourceArr[indexPath.row].read = 1;
    [self.tableView reloadData];
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - 加载数据
- (void)requestPassengerResourceData {

    WeakSelf
    [kNetworkManager requestHouseRecommendPassengerWithAhbpId:self.ahbpId success:^(NSArray<SISSPassengerResourceModel *> *passengerResourceArr) {
        
        weakSelf.noDataShowView.hidden = passengerResourceArr.count > 0;
        NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:passengerResourceArr.count];
        [passengerResourceArr enumerateObjectsUsingBlock:^(SISSPassengerResourceModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.isMain == 1) {
                weakSelf.passengerModel = obj;
            } else {
                [arrM addObject:obj];
            }
        }];
        weakSelf.passengerResourceArr = arrM.copy;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
        }];
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

@end
